<?php


namespace app\admin\controller\video;
use app\common\controller\Backend;
use app\common\model\VideoConfig;

class Manager extends Backend
{
    public function upload(){
        $file = request()->file('video');
        $name = request()->post("name");
        if($file){
            $info = $file->validate(['size'=>25000000,'ext'=>'mp4'])
                ->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $videoConfig =new  VideoConfig();
                $rs=$videoConfig->where("name","=",$name)->update([
                    "url"=>$info->getSaveName()
                ]);
                if ($rs){
                    $this->success("更新成功","index");
                }else{
                    $this->error("更新失敗");
                }
            }else{
                // 上传失败获取错误信息
                $this->error($file->getError());
            }
        }else{
            $this->error("文件上传失败");
            //$this->redirect("index");
        }
    }
    public function index()
    {
        if (!IS_AJAX){
            return $this->view->fetch();
        }else{
            $model = new VideoConfig();
            $dataList=$model->select();
            return json(['status'=>1,"data"=>$dataList]);
        }
    }
    public function setStatus($id,$status){
        $model = new VideoConfig();
        $rs=$model->where("id","=",$id)->update(["status"=>$status]);
        return json(['status'=>(bool)$rs?1:0]);
    }
}